package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.icss.beans.CoreCourseVO;
import com.icss.utils.RecordSet;

/**
 * @author 韦锦良
 *　类的作用：核心课程逻辑类
 * 最后修改时间：2010-3-31　下午12:49:21
 * 最后的改动内容：初始版本
 */
public class CoreCourseDao {

	private Dao dao = new Dao();

	private ResultSet rs = null;

	//通过课程编号获得些具体些课程编号的课程的相关信息
	public CoreCourseVO getCoreCourseVOById(String courseID) {

		CoreCourseVO vo = null;

		String sql = "select * from CoreCourse where courseID=" + courseID;

		rs = dao.query(sql);

		vo = getVOFromResultSet(rs).get(0);

		close();

		return vo;

	}

	//获得符合前台界面显示的核心课程
	public RecordSet getCourseForIndex() {

		RecordSet rs = null;
		String strSQL = "select * from CoreCourse order by courseId";
		rs = dao.queryPager(strSQL, 3, 1);
		close();
		return rs;
	}

	//从ResultSet里面将数据封装到vo类
	public ArrayList<CoreCourseVO> getVOFromResultSet(ResultSet rs) {

		ArrayList<CoreCourseVO> vos = new ArrayList<CoreCourseVO>();

		try {
			while (rs.next()) {
				CoreCourseVO vo = new CoreCourseVO();
				vo.setCourseID(rs.getInt("courseID"));
				vo.setCourseName(rs.getString("courseName"));
				vo.setCourseRemark(rs.getString("courseRemark"));
				vo.setCourseIntro(rs.getString("courseIntro"));
				vos.add(vo);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close();
		}

		return vos;

	}

		//获得所有的课程
	public ArrayList<CoreCourseVO> getAllCoreCourseVO() {

		ArrayList<CoreCourseVO> vos = null;

		String sql = "select * from CoreCourse order by courseID desc";

		rs = dao.query(sql);

		vos = getVOFromResultSet(rs);

		close();

		return vos;
	}

	//获得所有的课程的标题
	public ArrayList<CoreCourseVO> getAllTitle() {

		ArrayList<CoreCourseVO> vos = new ArrayList<CoreCourseVO>();

		String sql = "select courseID, courseName from CoreCourse order by courseID desc";

		rs = dao.query(sql);

		try {
			while (rs.next()) {
				CoreCourseVO vo = new CoreCourseVO();
				vo.setCourseID(rs.getInt("courseID"));
				vo.setCourseName(rs.getString("courseName"));
				vos.add(vo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		close();

		return vos;
	}

	//关闭连接
	public void close() {

		try {
			if (null != rs) {
				rs.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		dao.close();
	}

	//执行语句
	public boolean execute(String sql) {
		return dao.execute(sql);
	}

}
